package test.format_test;

public class ForThread {
    public static void main(String[] args) {
        T1 t1 = new T1();
        new Thread(t1).start();
        new Thread(t1).start();
    }
}

class T1 implements Runnable{
    int count = 0;

    void test(){
        synchronized (new T2()){
        System.out.println("11");
        System.out.println("22");}
        System.out.println("33");
    }


    synchronized void method(){

            System.out.println("1号任务 实质线程："+Thread.currentThread().getName());
            System.out.println(count++);


    }
    @Override
    public void run(){
        while (true)
        method();

    }
}

class T2 implements Runnable{

    @Override
    public void run() {
        while (true){
            System.out.println("2号任务  实质线程："+Thread.currentThread().getName());
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    }
}

class ForSyn{
    public static void main(String[] args) throws InterruptedException {
        Object lock = new Object();
        new Thread(){
            public void run(){
                synchronized (lock){
                    System.out.println("0线程获得锁");
                    try {
                        Thread.sleep(10000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("0线程释放锁");
                }
            }
        }.start();
        Thread.sleep(10);
        synchronized (lock){
            System.out.println("main线程获得锁");
        }
    }
}